package com.coastline.fruit.common.email;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Component
public class MailUtil {

    @Resource
    private JavaMailSender mailSender;

    /**
     * 发送简单邮件信息
     *
     * @param from 发送邮箱
     * @param to 接受邮箱
     * @param subject 邮件主题
     * @param text 邮件内容
     * @throws MessagingException 异常信息
     */
    public void sendSimpleMail(String from, String to, String subject, String text) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text);
        mailSender.send(message);
    }

    /**
     * 发送html模板类型的邮件
     *
     * @param from 发送邮箱
     * @param to 接受邮箱
     * @param subject 邮件主题
     * @param html 邮件内容
     * @throws MessagingException 异常信息
     */
    public void sendHtmlMail(String from, String to, String subject, String html) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(html, true);
        mailSender.send(message);
    }
}
